from fastapi import FastAPI, HTTPException, Depends
from fastapi.middleware.cors import CORSMiddleware
from typing import List, Dict, Any
from pydantic import BaseModel
import uvicorn

# Import internal modules
from app.api.routes import router
from app.core.config import settings
from app.core.db_engine import init_db

# Initialize FastAPI app
app = FastAPI(
    title="RAG Text2SQL评估系统",
    description="基于LLM的SQL生成准确率评估工具",
    version="1.0.0"
)

# Add CORS middleware to allow frontend access
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # Allows all origins
    allow_credentials=True,
    allow_methods=["*"],  # Allows all methods
    allow_headers=["*"],  # Allows all headers
)

# Initialize database
@app.on_event("startup")
async def startup_event():
    await init_db()

# Include API routes
app.include_router(router)

# Root endpoint
@app.get("/")
def read_root():
    return {"message": "RAG Text2SQL评估系统后端服务已启动"}

# 确保应用可以通过uvicorn命令正确运行
# 使用命令: uvicorn main:app --reload --host 0.0.0.0 --port 8000
# 或在开发环境中直接运行: python main.py
if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)